﻿@using System.Reflection
@using System.Linq.Expressions
@using System.ComponentModel.DataAnnotations
@
@using Whoever.Framework.Base
@using Whoever.Framework.Attributes
@using Whoever.Patterns.Website2.Base
@model Whoever.Patterns.Website2.Base.PresentationModel
@{
    Type baseEntity = Model.ViewModel.Entity.GetType();
}
@{
    ViewBag.Title = string.Format("Crear {0}", Model.ViewModel.EntityDisplayName);
}

<div class="page-header">
    <h1>Crear @Model.ViewModel.EntityDisplayName</h1>
</div>

@using (Html.BeginForm("Create", baseEntity.Name, FormMethod.Post, new { @class = "form-horizontal" }))
{
    <fieldset>
        <legend>Datos de @Model.ViewModel.EntityDisplayName</legend>
        @Html.ValidationSummary(true)
        @foreach (var property in Model.ViewModel.PropertyList.Where(p => !p.GetGetMethod().IsVirtual && p.GetCustomAttributes(typeof(NotMappedAttribute), true).Count() == 0 && p.GetCustomAttributes(typeof(IdentityField), true).Count() == 0 && p.GetCustomAttributes(typeof(ForeignKeyField), true).Count() == 0))
        {   
            @Html.Partial("_FormField", new FieldModel { ViewModel = Model.ViewModel, Property = property });
        }
        @foreach (var property in Model.ViewModel.GetType().GetProperties().Where(p => p.GetCustomAttributes(typeof(SelectableRelationship), true).Length != 0))
        {
            SelectableRelationship attribute = property.GetCustomAttributes(typeof(SelectableRelationship), true).First() as SelectableRelationship;
            @Html.Partial("_FormCombo", new ComboModel { ViewModel = Model.ViewModel, PropertyList = property, PropertyForeignKey = Model.ViewModel.Entity.GetType().GetProperty(attribute.ForeignKeyName), PropertyListKey = attribute.RelationKeyName, PropertyListValue = attribute.RelationValueName, IsRequired = attribute.IsRequired });
        }
    </fieldset>
    <div class="form-buttons">
        <input type="submit" value="Guardar" class="btn btn-primary btn-large" id="btnCreate" />
    </div>
    <p>
        <a href="#" onclick="history.go(-1); return false;">Cancelar y volver</a>
    </p>
}
